package com.hy.study.inputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * 这里一个文件调用一次
 * @author huyong
 * @date 2024 年 01 月 31 日 10:45: 46
 */
public class WholeRecordReader extends RecordReader<Text, BytesWritable> {
    private FileSplit split;
    private Configuration configuration;
    private final Text key = new Text();
    private final BytesWritable value = new BytesWritable();

    private boolean isProgress = true;

    /**
     * 初始化方法
     *
     * @param split   the split that defines the range of records to read
     * @param context the information about the task
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        this.split = (FileSplit) split;// 表示每一个小文件
        this.configuration = context.getConfiguration();
    }

    /**
     * 核心业务逻辑
     *
     * @return
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if (isProgress) {
            // 获取fs 对象
            Path path = split.getPath();
            FileSystem fs = path.getFileSystem(configuration);
            // 获取输入流 读取每一个切片文件
            FSDataInputStream inputStream = fs.open(path);
            // 拷贝
            byte[] buff = new byte[(int) split.getLength()];
            // 读取到缓冲区中
            IOUtils.readFully(inputStream, buff, 0, buff.length);
            // 写入到value中
            value.set(buff, 0, buff.length);
            // 封装key
            key.set(path.toString());// 路径+文件名称
            // 关闭资源
            IOUtils.closeStream(inputStream);
            isProgress = false;
            return true;
        }

        return false;
    }

    /**
     * 获取当前的Key
     *
     * @return
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return key;
    }

    /**
     * 获取当前的value
     *
     * @return
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return value;
    }

    /**
     * 处理的精度条
     *
     * @return
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    /**
     * 关闭资源
     *
     * @throws IOException
     */
    @Override
    public void close() throws IOException {

    }
}
